package org.sunnyleaf.votedemo.client.data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@SuppressWarnings("serial")
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class VoteItem implements Serializable {

	public static final String SINGLE = "single";
	public static final String MULTI = "multi";

	public VoteItem(){
		this.itemArray = new ArrayList<String>();
		this.countsArray = new ArrayList<Integer>();
	}
	
	public VoteItem(Long voteID, String voteContent, String type, boolean canAbstain) {
		this.voteID = voteID;
		this.question = voteContent;
		this.type = type;
		this.abstainable = canAbstain;
		this.numAbstain = 0;
		this.numPolled = 0;
		this.countsArray = new ArrayList<Integer>();
		this.itemArray = new ArrayList<String>();
	}
	

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;

	@Persistent
	private Long voteID;

	@Persistent
	private String question;

	// 用String目的是放到数据库里之后看起来方便
	@Persistent
	private String type; // 单选还是多选

	@Persistent
	private boolean abstainable; // 能否弃权

	@Persistent
	/**
	 * @gwt.typeArgs <java.lang.String>
	 */
	private List<String> itemArray; // 选项

	
	@Persistent
	/**
	 * @gwt.typeArgs <java.lang.Integer>
	 */
	private List<Integer> countsArray ; // 各个选项选择的人数

	@Persistent
	private int numPolled; // 投票数

	@Persistent
	private int numAbstain; // 弃权数

	// 增加投票人数
	public void incPolled(){
		numPolled++;
	}
	
	public void addItem(String item) {
		itemArray.add(item);
		countsArray.add(0);
	}

	public void setItemCount( int index, Integer count ){
		this.countsArray.set(index, count);
	}

	public void incItemCount(int index) {
		Integer count = countsArray.get(index);
		countsArray.set(index, count + 1);
	}

	public void incAbstainCount() {
		this.numAbstain++;
	}

	/**
	 * @param abstainable
	 *            the abstainable to set
	 */
	public void setAbstainable(boolean abstainable) {
		this.abstainable = abstainable;
	}

	/**
	 * @return the abstainable
	 */
	public boolean isAbstainable() {
		return abstainable;
	}

	/**
	 * @param numPolled
	 *            the numPolled to set
	 */
	public void setNumPolled(int numPolled) {
		this.numPolled = numPolled;
	}

	/**
	 * @return the numPolled
	 */
	public int getNumPolled() {
		return numPolled;
	}

	/**
	 * @param numAbstain
	 *            the numAbstain to set
	 */
	public void setNumAbstain(int numAbstain) {
		this.numAbstain = numAbstain;
	}

	/**
	 * @return the numAbstain
	 */
	public int getNumAbstain() {
		return numAbstain;
	}

	/**
	 * @param countsArray
	 *            the countsArray to set
	 */
	public void setCountsArray(List<Integer> countsArray) {
		this.countsArray = countsArray;
	}

	/**
	 * @return the countsArray
	 */
	public List<Integer> getCountsArray() {
		return countsArray;
	}

	/**
	 * @param content
	 *            the content to set
	 */
	public void setContent(String content) {
		this.question = content;
	}

	/**
	 * @return the content
	 */
	public String getContent() {
		return question;
	}

	/**
	 * @return the voteID
	 */
	public Long getVoteID() {
		return voteID;
	}

	/**
	 * @param voteID
	 *            the voteID to set
	 */
	public void setVoteID(Long voteID) {
		this.voteID = voteID;
	}

	/**
	 * @return the question
	 */
	public String getQuestion() {
		return question;
	}

	/**
	 * @param question
	 *            the question to set
	 */
	public void setQuestion(String question) {
		this.question = question;
	}

	/**
	 * @return the type
	 */
	public String getType() {
		return type;
	}

	/**
	 * @param type
	 *            the type to set
	 */
	public void setType(String type) {
		this.type = type;
	}

	/**
	 * @return the itemArray
	 */
	public List<String> getItemArray() {
		return itemArray;
	}

	/**
	 * @param itemArray
	 *            the itemArray to set
	 */
	public void setItemArray(List<String> itemArray) {
		this.itemArray = itemArray;
	}

	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}

}
